package com.example.ymm.component.callback.chatbot;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.open.app.api.GenericEventListener;
import com.dingtalk.open.app.api.OpenDingTalkStreamClientBuilder;
import com.dingtalk.open.app.api.message.GenericOpenDingTalkEvent;
import com.dingtalk.open.app.api.security.AuthClientCredential;
import com.dingtalk.open.app.stream.protocol.event.EventAckStatus;
import com.example.ymm.modules1.entity.my.sys.DdEventLog;
import com.example.ymm.modules1.service.sys.DdEventLogService;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;




/**
 * 钉钉事件监听
 */
@Slf4j
@Component
public class DingTalkStreamListener {

    @Value("${dd.appKey}")
    private String appKey;//Client ID (原 AppKey 和 SuiteKey)

    @Value("${dd.appSecret}")
    private String appSecret;//Client Secret (原 AppSecret 和 SuiteSecret)
    @Value("${spring.profiles.active}")
    String PD_MATCH;


    @Autowired
    DdEventLogService ddEventLogService;
    /**
     * 监听事件 和消费事件
     */
    @PostConstruct
    public void initFn() {
        try{
            if ("prod-new".equals(PD_MATCH)) {
                System.out.println("-----正在注册中:");
                OpenDingTalkStreamClientBuilder
                        .custom()
                        .credential(new AuthClientCredential(appKey, appSecret))
                        //注册事件监听
                        .registerAllEventListener(new GenericEventListener() {
                            public EventAckStatus onEvent(GenericOpenDingTalkEvent event) {
                                try {
                                    //事件唯一Id
                                    String eventId = event.getEventId();
                                    //事件类型
                                    String eventType = event.getEventType();
                                    System.out.println("---eventType:"+eventType);
                                    //事件产生时间
                                    Long bornTime = event.getEventBornTime();
                                    //获取事件体
                                    JSONObject bizData = event.getData();
                                    //处理事件
                                    process(bizData);

                                    //添加记录
                                    DdEventLog eventLog=new DdEventLog();
                                    eventLog.setEventId(eventId);
                                    eventLog.setEventType(eventType);
                                    eventLog.setData(bizData.toJSONString());
                                    ddEventLogService.saveOrUpdate(eventLog);
                                    //消费成功
                                    return EventAckStatus.SUCCESS;
                                } catch (Exception e) {
                                    //消费失败
                                    return EventAckStatus.LATER;
                                }
                            }
                        })
                        .build().start();
            }

        }catch (Exception e){
            log.error("--钉钉事件注册异常:"+e.getMessage(),e);
        }

    }


    public void process(JSONObject json){
        log.info("---钉钉事件事件报文："+json);
    }


}
